﻿using KingsSharp.Common.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CRM.Admin
{
    public enum LayerIcon
    {
        Add = 0,
        Edit = 1,
        Delete = 2,
        Details = 3,
        None=4
    }
    /// <summary>
    /// HTML扩展
    /// </summary>
    public static class LogisticsHelper
    {
        /// <summary>
        /// 设置头部导航信息
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static HtmlString GetGridTitle(this HtmlHelper helper, params string[] obj)
        {
            string title = "<span class=\\\"p-icon-home\\\" style=\\\"padding-left:20px\\\">";
            for (int i = 0; i < obj.Length; i++)
            {
                if (i == 0)
                {
                    title = title + obj[i];
                }
                else
                {
                    title = title + " &nbsp;<span>-></span>&nbsp;" + obj[i];
                }
            }
            return new HtmlString(title);
        }
        /// <summary>
        /// 设置layer title
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="icon"></param>
        /// <param name="layertitle"></param>
        /// <returns></returns>
        public static HtmlString GetLayerTitle(this HtmlHelper helper, LayerIcon icon, string layertitle)
        {
            string iconstr = "&#xe61d;", title = "";
            switch (icon)
            {
                case LayerIcon.Add:
                    iconstr = "&#xe63c;";
                    break;
                case LayerIcon.Edit:
                    iconstr = "&#xe692;";
                    break;
                case LayerIcon.Delete:
                    iconstr = "&#xe62e;";
                    break;
                case LayerIcon.Details:
                    iconstr = "&#xe616;";
                    break;
            }
            if(icon!= LayerIcon.None)
            {
                title = "<i class=\\\"icon iconfont\\\" style=\\\"font-size:18px\\\">" + iconstr + "</i><span style=\\\"font-size:14px;margin-left:5px;\\\">" + layertitle + "</span>";
            }
            else
            {
                title = "<span style=\\\"font-size:14px;margin-left:5px;\\\">" + layertitle + "</span>";
            }
            return new HtmlString(title);
        }
        /// <summary>
        /// 设置layer title
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="icon">0新增、1编辑、2删除、3详情</param>
        /// <param name="layertitle"></param>
        /// <returns></returns>
        public static HtmlString GetLayerTitle(this HtmlHelper helper, int icon, string layertitle)
        {
            LayerIcon layertype = LayerIcon.Details;
            switch (icon)
            {
                case 0:
                    layertype = LayerIcon.Add;
                    break;
                case 1:
                    layertype = LayerIcon.Edit;
                    break;
                case 2:
                    layertype = LayerIcon.Delete;
                    break;
                case 3:
                    layertype = LayerIcon.Details;
                    break;
            }
            return GetLayerTitle(helper, layertype, layertitle);
        }
        /// <summary>
        /// 构造适用filter操作符的html字符串
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="title">操作标识,对应表单项name值</param>
        /// <param name="operates">fliter操作数组</param>
        /// <param name="defaultoperate">默认的操作选项,不传则为Equal</param>
        /// <returns>适用filter操作符的html字符串</returns>
        public static HtmlString GetFilterItem(this HtmlHelper helper, string title, FilterOperate[] operates, FilterOperate defaultoperate = FilterOperate.Equal)
        {
            operates = operates.Distinct().ToArray();
            string filterstr = "<div class=\"easyui-menu\" style=\"width:150px;display:none;\">";
            for (int i = 0; i < operates.Length; i++)
            {
                string stroption = "";
                if (operates[i] == defaultoperate)
                {
                    stroption = "iconCls:'icon-ok',";
                }
                stroption += "name:'" + operates[i].ToOperateCode() + "'";
                filterstr += "<div data-source-obj=\"operate_" + title + "\" data-options=\"" + stroption + "\">" + operates[i].ToOperateName() + "</div>";
            }
            filterstr += "<input type=\"hidden\" id=\"operate_" + title + "\" name=\"operate_" + title + "\" value=\"" + defaultoperate.ToOperateCode() + "\" /></div>";
            return new HtmlString(filterstr);
        }
        /// <summary>
        /// 构造适用filter操作符的html字符串
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="title">操作标识,对应表单项name值</param>
        /// <param name="type">根据类型自动构造操作选项数组</param>
        /// <param name="defaultoperate">默认的操作选项,不传则为Equal</param>
        /// <returns>适用filter操作符的html字符串</returns>
        public static HtmlString GetFilterItem(this HtmlHelper helper, string title, Type type, FilterOperate defaultoperate = FilterOperate.Contains)
        {
            List<FilterOperate> operates = new List<FilterOperate>();
            if (type == typeof(int) || type == typeof(float)
                || type == typeof(long) || type == typeof(short) || type == typeof(DateTime))
            {
                defaultoperate = defaultoperate == FilterOperate.Contains ? FilterOperate.Equal : defaultoperate;
                operates.AddRange(new[] { FilterOperate.Equal, FilterOperate.NotEqual, FilterOperate.Less, FilterOperate.LessOrEqual, FilterOperate.Greater, FilterOperate.GreaterOrEqual });
            }
            else if (type == typeof(string))
            {
                operates.AddRange(new[] { FilterOperate.Equal, FilterOperate.NotEqual, FilterOperate.Contains, FilterOperate.StartsWith, FilterOperate.EndsWith });
            }
            else
            {
                defaultoperate = defaultoperate == FilterOperate.Contains ? FilterOperate.Equal : defaultoperate;
                operates.AddRange(new[] { FilterOperate.Equal, FilterOperate.NotEqual });
            }
            return GetFilterItem(helper, title, operates.ToArray(), defaultoperate);
        }

        public delegate void SelfApplicable<T>(SelfApplicable<T> self, T arg);

        public static void Render<T>(this HtmlHelper helper, T model, SelfApplicable<T> f)
        {
            f(f, model);
        }
    }
}